package cn.wangzg.mymusic;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class MusicUtils {
    public static List<Music> getMusic(Context context){
        List<Music> musics=new ArrayList<>();
        String[] projection = {
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.DURATION,
                MediaStore.Audio.Media.ALBUM_ID
        };
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                projection, null, null, null);
        while (cursor.moveToNext()){
            String title = cursor.getString(0);
            String artist = cursor.getString(1);
            String data = cursor.getString(2);
            long duration = cursor.getLong(3);
            int albumId = cursor.getInt(4);

            System.out.println(title+","+artist);
            musics.add(new Music(title,artist,data,duration,albumId));
        }
        cursor.close();
        return musics;
    }

    public static String formatTime(long ms){
        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
        return sdf.format(ms);
    }
}
